package com.liupengyuan.framework.util;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public class BeanMapUtil {

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static Map bean2Map(Object object){
		Field[] field = object.getClass().getDeclaredFields();
		Map map = new HashMap();
		for(int i=0;i<field.length;i++){  
            Method method;
			try {
				method = object.getClass().getMethod(getGetMethodName(field[i].getName()));
		
				Object value = method.invoke(object);
				map.put(field[i].getName(), value);
			} catch (SecurityException e1) {
				e1.printStackTrace();
			} catch (NoSuchMethodException e1) {
				e1.printStackTrace();
			}  catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			}  
		}  
		return map;
	}
	
	
	/** 
     * 将field名改为其getter方法名
     * @param field 
     * @return 
     */  
    public static String getGetMethodName(String field) {  
        return "get"+field.substring(0, 1).toUpperCase()+field.substring(1);  
    } 
}
